﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SyndicateDAO.DatabaseObject;
using System.Web.UI.HtmlControls;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class TabAddPlayer : Base
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;

            SetList();
        }

        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            if (this.DrawLocked)
                this.BasePage.Redirect(String.Format("/Secured/Draws/?dId={0}&Tab=4", this.CurrentDraw.id));

            BindList();
        }

        private void BindList()
        {
            int minimumFunds = 1;
            if (this.CurrentDraw.type == draw.drawType.EUROMILLIONS)
                minimumFunds = 2;

            if ((int) CurrentDraw.pricePerLine != 0)
            {
                minimumFunds = (int) CurrentDraw.pricePerLine;
            }


            var query = this.BasePage.BusinessFacade.DrawBL.GetUnassignedUsers(CurrentDraw.id).Where(u => u.funds >= minimumFunds && u.isEnabled).OrderBy(p => p.username);

            if (query.Count() > 0)
            {
                this.RepeaterList.DataSource = query;
                this.RepeaterList.ItemDataBound += new RepeaterItemEventHandler(RepeaterList_ItemDataBound);
                this.RepeaterList.DataBind();

                this.MultiViewList.SetActiveView(this.ViewList);
            }
            else
            {
                this.MultiViewList.SetActiveView(this.ViewEmpty);
            }
        }

        void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            user u = e.Item.DataItem as user;

            ((System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Item.FindControl("chkAction")).Value = String.Format("userId_{0}", u.id);
        }

        protected string PrintPlayerName(Object data)
        {
            user p = data as user;

            return p.username;

        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            bool actionPeformed = false;
           


            ControlCollection repeaterRows = this.FindControl("RepeaterList").Controls;
            HtmlInputCheckBox cb;
            foreach (Control row in repeaterRows)
            {
                cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("chkAction");
                if (cb.Checked)
                {
                    int? ObjectId = Utilities.StringToInt(cb.Value.Replace("userId_", ""));

                    if (ObjectId.HasValue)
                    {
                        user u = this.BasePage.BusinessFacade.UserBL.GetSingleUser(ObjectId.Value);

                        player p = new player();

                        p.drawId = CurrentDraw.id;
                        p.userId = u.id;

                        if (this.BasePage.BusinessFacade.PlayerBL.InsertPlayer(p))
                        {
                            InsertEventLogEntry("Player [" + p.id.ToString() + "] added.");

                            if ((int)this.CurrentDraw.pricePerLine == 0)
                            {
                                if (this.CurrentDraw.type == draw.drawType.EUROMILLIONS)
                                    u.funds = u.funds - 2;
                                else
                                    u.funds = u.funds - 1;
                            }
                            else
                            {
                                u.funds = u.funds - (int)this.CurrentDraw.pricePerLine;
                            }

                            if (this.BasePage.BusinessFacade.UserBL.UpdateUser())
                            {
                                fundsLog logEntry = new fundsLog();

                                logEntry.moneyIn = false;

                                logEntry.fromUserId = this.BasePage.LoggedInUser.id;
                                logEntry.toUserId = u.id;
                                logEntry.dateCreated = DateTime.Now;

                                if ((int) this.CurrentDraw.pricePerLine == 0)
                                {
                                    if (this.CurrentDraw.type == draw.drawType.EUROMILLIONS)
                                        logEntry.amount = 2;
                                    else
                                        logEntry.amount = 1;
                                }
                                else
                                {
                                    logEntry.amount = (int)this.CurrentDraw.pricePerLine;
                                }

                                logEntry.syndicateId = this.BasePage.LoggedInUser.syndicateId;
                                logEntry.description = String.Format("Player added to draw {0}", PrintDrawDate());

                                this.BasePage.BusinessFacade.FundsLogBL.InsertEntry(logEntry);

                                InsertEventLogEntry("User [" + u.id.ToString() + "] funds updated.");
                            }

                            if (!actionPeformed)
                                actionPeformed = true;
                        }
                    }
                }

            }

            if (actionPeformed)
            {
                
                this.BindList();
            }
        }
    }
}